package com.definesys.smecrailtransit.module.MonthForm;

import android.content.Context;

import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.BaseSubscriber;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.HttpResult;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBasePresenter;
import com.definesys.smecrailtransit.bean.ApplyBean;
import com.definesys.smecrailtransit.bean.LineBean;
import com.definesys.smecrailtransit.bean.StationBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.CarePlanBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.FaultLogBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.MonthFormBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.PositionBean;
import com.definesys.smecrailtransit.module.MonthForm.dialog.MonthFormDialog;

import java.util.ArrayList;
import java.util.HashMap;

import io.realm.RealmResults;
import rx.Observable;

/**
 * Created by xupeizuo on 2017/7/20.
 */

public class MonthFormPresenter extends SmecBasePresenter implements MonthFormContract {

    public MonthFormPresenter(Context mContext) {
        super(mContext);
    }

    @Override
    public void getMonthForm(HashMap<String, String> map) {
        Observable<HttpResult<MonthFormBean>> observable=smecRailService.monthForm(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<MonthFormBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MonthFormFragment.GET_MONTH_FORM_ERROR,new Object());
            }

            @Override
            public void onNext(HttpResult<MonthFormBean> arrayListHttpResult) {
                MonthFormBean arrayList=arrayListHttpResult.getData();
                ArrayList<LineBean> lineList = arrayList.getLine();
                ArrayList<ApplyBean> applyList = arrayList.getApply();
                ArrayList<StationBean> stationList = arrayList.getStation();

                for (LineBean lineBean : lineList) {
                    lineBean.setFlag(SmecRailApi.MonthCode.MONTHFORM);
                }
                for (ApplyBean applyBean : applyList) {
                    applyBean.setFlag(SmecRailApi.MonthCode.MONTHFORM);
                }
                for (StationBean stationBean : stationList) {
                    stationBean.setFlag(SmecRailApi.MonthCode.MONTHFORM);
                }

                realmManager.insertOrUpdateBatch(lineList);
                realmManager.insertOrUpdateBatch(applyList);
                realmManager.insertOrUpdateBatch(stationList);
                realmManager.insertOrUpdateBatch(arrayList.getStatistics());
                mRxbus.post(MonthFormFragment.GET_MONTH_FORM_SUCCESS,arrayList);
            }
        });
    }

    @Override
    public void getStationForm(HashMap<String, Object> map) {
        Observable<HttpResult<ArrayList<PositionBean>>> observable=smecRailService.stationForm(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<PositionBean>>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MonthFormDialog.GET_STATION_FORM_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<ArrayList<PositionBean>> arrayListHttpResult) {
                ArrayList<PositionBean> arrayList=arrayListHttpResult.getData();
                mRxbus.post(MonthFormDialog.GET_STATION_FORM_SUCCESS,arrayList);
            }
        });
    }

    @Override
    public void getFaultLog(HashMap<String, Object> map) {
        Observable<HttpResult<FaultLogBean>> observable=smecRailService.faultLog(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<FaultLogBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MonthFormDialog.GET_FAULT_LOG_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<FaultLogBean> arrayListHttpResult) {
                FaultLogBean arrayList= arrayListHttpResult.getData();
                mRxbus.post(MonthFormDialog.GET_FAULT_LOG_SUCCESS,arrayList);
            }
        });
    }

    @Override
    public void getCarePlan(HashMap<String, Object> map) {
        Observable<HttpResult<ArrayList<CarePlanBean>>> observable=smecRailService.carePlan(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<CarePlanBean>>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(MonthFormDialog.GET_CARE_PLAN_FAILED,"");
            }

            @Override
            public void onNext(HttpResult<ArrayList<CarePlanBean>> arrayListHttpResult) {
                ArrayList<CarePlanBean> arrayList=arrayListHttpResult.getData();
                mRxbus.post(MonthFormDialog.GET_CARE_PLAN_SUCCESS,arrayList);
            }
        });
    }


    @Override
    public ArrayList<LineBean> getLineBeanFromRealm() {
        RealmResults<LineBean> realTimeBeanRealmResults = (RealmResults<LineBean>) realmManager.findAll(LineBean.class);
        if(realTimeBeanRealmResults == null){
            return null;
        }
        return (ArrayList<LineBean>)realmManager.getRealm().copyFromRealm(realTimeBeanRealmResults);
    }

    @Override
    public ArrayList<ApplyBean> getApplyBeanFromRealm() {
        RealmResults<ApplyBean> realTimeBeanRealmResults = (RealmResults<ApplyBean>) realmManager.findAll(ApplyBean.class);
        if(realTimeBeanRealmResults == null){
            return null;
        }
        return (ArrayList<ApplyBean>)realmManager.getRealm().copyFromRealm(realTimeBeanRealmResults);
    }
//
    @Override
    public ArrayList<StationBean> getStationBeanFromRealm() {
        RealmResults<StationBean> realTimeBeanRealmResults = (RealmResults<StationBean>) realmManager.findAll(StationBean.class);
        if(realTimeBeanRealmResults == null){
            return null;
        }
        return (ArrayList<StationBean>)realmManager.getRealm().copyFromRealm(realTimeBeanRealmResults);
    }



}
